package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    //注入远程调用接口
    @Autowired
    private CustomerInfoFeignClient client;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CustomerInfoFeignClient customerInfoFeignClient;

    /**
     * @description: 根据后台登录的code,登录小程序
     * @author: xiaoxie
     * @date: 2024/10/23 20:15
     * @param: [code]
     * @return: token
     **/
    @Override
    public String login(String code) {
        //1.远程调用 service-client 的接口
        Result<Long> result = client.login(code);
        log.info("远程调用的结果为: {}",result);
        Long loginId = result.getData();
        //2.校验远程调用的结果
        if(result.getCode() != 200) {
            log.warn(ResultCodeEnum.FEIGN_FAIL.getMessage());
            throw new GuiguException(ResultCodeEnum.FEIGN_FAIL);
        }
        if(null == loginId ) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        //3.生成token
        String token = UUID.randomUUID().toString().replaceAll("-","");
        //4..存在redis中
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token
                                 ,loginId.toString(),RedisConstant.USER_LOGIN_KEY_TIMEOUT
                                 ,TimeUnit.MINUTES);

        return token;
    }
    /**
     * @description: 根据id 获取用户的详细信息
     * @author: xiaoxie
     * @date: 2024/10/23 20:19
     * @param: [customerId] id
     * @return: CustomerLoginVo 用户的详细信息
     **/
    //获取用户信息
    @Override
    public CustomerLoginVo getCustomerInfo(Long customerId) {

        //根据用户id进行远程调用 得到用户信息
        Result<CustomerLoginVo> customerLoginVoResult =
                customerInfoFeignClient.getCustomerLoginInfo(customerId);

        Integer code = customerLoginVoResult.getCode();
        if(code != 200) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        CustomerLoginVo customerLoginVo = customerLoginVoResult.getData();
        if(customerLoginVo == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //5 返回用户信息
        return customerLoginVo;
    }

    //更新用户微信手机号
    @Override
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        Result<Boolean> booleanResult = customerInfoFeignClient.updateWxPhoneNumber(updateWxPhoneForm);
        return true;
    }

}
